package com.amqr.movetest.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.amqr.movetest.R;
import com.amqr.movetest.bean.SimpleVpBean;

import java.util.ArrayList;
import java.util.List;

/**
 * User: LJM
 * Date&Time: 2017-05-17 & 20:37
 * Describe: Describe Text
 */
public class SimpleVpActivity extends AppCompatActivity {
    private ViewPager mVpSimple;
    List<SimpleVpBean> simpleVpBeanList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_vp);
        mVpSimple = (ViewPager) findViewById(R.id.mVpSimple);

        getDataSource();
        MyPagerAdapter myPagerAdapter = new MyPagerAdapter(simpleVpBeanList);
        mVpSimple.setAdapter(myPagerAdapter);

        mVpSimple.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Log.d("TTT","子vp：onPageSelected  "+position);

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                switch (state) {
                    // 拖动中
                    case ViewPager.SCROLL_STATE_DRAGGING:
                        Log.d("TTT","vp：SCROLL_STATE_======DRAGGING");
                        break;
                    // 滑动结束
                    case ViewPager.SCROLL_STATE_SETTLING:
                        Log.d("TTT","vp：SCROLL_STATE_======SETTLING");
                        break;

                    // 闲置中
                    //
                    case ViewPager.SCROLL_STATE_IDLE:
                        Log.d("TTT","vp：SCROLL_STATE_======IDLE");
                        break;

                }
            }
        });
    }



    public class MyPagerAdapter extends PagerAdapter {
        List<SimpleVpBean> beanList;
        public MyPagerAdapter() {
        }

        public MyPagerAdapter(List<SimpleVpBean> simpleVpBeanList) {
            super();
            this.beanList = simpleVpBeanList;
        }

        @Override
        public int getCount() {
            return beanList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            View view = View.inflate(SimpleVpActivity.this, R.layout.item_simple_vp_page, null);
            SimpleVpBean simpleVpBean = beanList.get(position);
            if(position%2==0){
                view.setBackgroundColor(ContextCompat.getColor(SimpleVpActivity.this,R.color.colorAccent));
            }else{
                view.setBackgroundColor(ContextCompat.getColor(SimpleVpActivity.this,R.color.colorPrimary));
            }

            TextView mTvTop = (TextView) view.findViewById(R.id.mTvTop);
            TextView mTvBot = (TextView) view.findViewById(R.id.mTvBot);
            mTvTop.setText(simpleVpBean.top);
            mTvBot.setText(simpleVpBean.bot);

            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }



    private void getDataSource() {
        simpleVpBeanList = new ArrayList<>();
        SimpleVpBean simpleVpBean0 = new SimpleVpBean();
        simpleVpBean0.top ="page0";
        simpleVpBean0.bot ="孙悟空";

        SimpleVpBean simpleVpBean1 = new SimpleVpBean();
        simpleVpBean1.top ="page1";
        simpleVpBean1.bot ="路飞";

        SimpleVpBean simpleVpBean2 = new SimpleVpBean();
        simpleVpBean2.top ="page2";
        simpleVpBean2.bot ="马三立";

        simpleVpBeanList.add(simpleVpBean0);
        simpleVpBeanList.add(simpleVpBean1);
        simpleVpBeanList.add(simpleVpBean2);
    }
}
